#!/usr/bin/env python
#coding=UTF-8
'''
Created on 2010-11-9

@author: zarra
'''
from common import Question,copy_image
from numpy import mat,abs
from q3_spatial_filtering.q3 import Spatial_Filter


class Laplacian(Spatial_Filter):
    def __init__(self,mask):
        super(self.__class__,self).__init__(mask)
    def process(self,v):
        return reduce(lambda a,b:a+b, v)

class Sobel_x(Spatial_Filter):
    def __init__(self):
        m=[
           [-1,0,1],
           [-2,0,2],
           [-1,0,1],
           ]
        super(self.__class__,self).__init__(mat(m))
    def process(self,v):
        return reduce(lambda a,b:a+b, v)

    
class Sobel_y(Spatial_Filter):
    def __init__(self):
        m=[
           [1,2,1],
           [0,0,0],
           [-1,-2,-1],
           ]
        super(self.__class__,self).__init__(mat(m))
    def process(self,v):
        return  reduce(lambda a,b:a+b, v)



class Prewitt_x(Spatial_Filter):
    def __init__(self):
        m=[
           [-1,0,1],
           [-1,0,1],
           [-1,0,1],
           ]
        super(self.__class__,self).__init__(mat(m))
    def process(self,v):
        return reduce(lambda a,b:a+b, v)

    
class Prewitt_y(Spatial_Filter):
    def __init__(self):
        m=[
           [1,1,1],
           [0,0,0],
           [-1,-1,-1],
           ]
        super(self.__class__,self).__init__(mat(m))
    def process(self,v):
        return  reduce(lambda a,b:a+b, v)


class Roberts(Spatial_Filter):
    def __init__(self):
        m=[
           [0,0,0],
           [0,1,1],
           [0,1,1],
           ]
        super(self.__class__,self).__init__(mat(m))
    def process(self,v):
        return  abs(v[8]-v[4])+abs(v[7]-v[5])

    
class Q7(Question):
    def __init__(self,image,m='show'):
        super(self.__class__,self).__init__('Q7',image,m)
    
    def laplacian(self):
        image = self.image
        m=[
           [-1,-1,-1],
           [-1,8,-1],
           [-1,-1,-1],
           ]
        l=Laplacian(mat(m))
        dst = l(image)
        
        self.display(dst, 'Laplacian')
    
    def sobel(self):
        
        image = self.image
        sx= Sobel_x()
        sy= Sobel_y()
        
        dst_x =sx(image)
        dst_y =sy(image)
        
        dst = copy_image(image)
        for i in range(image.height):
            for j in range(image.width):
                dst[i,j]=abs(dst_x[i,j])+abs(dst_y[i,j])
        self.display(dst, 'Sobel')
        
        
    def prewitt(self):
        
        image = self.image
        px= Prewitt_x()
        py= Prewitt_y()
        
        dst_x =px(image)
        dst_y =py(image)
        
        dst = copy_image(image)
        for i in range(image.height):
            for j in range(image.width):
                dst[i,j]=abs(dst_x[i,j])+abs(dst_y[i,j])
        self.display(dst, 'Prewitt')
        
    def roberts(self):      
        image = self.image
        r = Roberts()
        dst = r(image)
        
        self.display(dst, 'Roberts')
          
    def action(self):
        self.laplacian()
        self.prewitt()
        self.sobel()
        self.roberts()
    
if __name__ == '__main__':
    q=Q7('fig3.jpg','save')
    q()
